#############################################################
#
# Driver for ralink-usb-wifi module
#
#############################################################

RT25USB_VERSION=2.0.8.0
RT25USB_SOURCE:=RT25USB-SRC-V$(RT25USB_VERSION).tar.gz
RT25USB_SITE:=http://www.ralinktech.com.tw/data
RT25USB_DIR=$(BUILD_DIR)/RT25USB-SRC-V$(RT25USB_VERSION)
RT25USB_CAT:=zcat
RT25USB_MODULE=$(RT25USB_DIR)/rt2570.ko
RT25USB_TARGET_MODULE=$(TARGET_DIR)/lib/modules/2.6.21gum/kernel/drivers/net/wireless/rt2570.ko
RT25USB_TARGET_CONF=$(BUILD_DIR)/.rt25usb


$(DL_DIR)/$(RT25USB_SOURCE):
	 $(WGET) -P $(DL_DIR) $(RT25USB_SITE)/$(RT25USB_SOURCE)

rt25usb-source: $(DL_DIR)/$(RT25USB_SOURCE)

$(RT25USB_DIR)/.unpacked: $(DL_DIR)/$(RT25USB_SOURCE)
	$(RT25USB_CAT) $(DL_DIR)/$(RT25USB_SOURCE) | tar -C $(BUILD_DIR) $(TAR_OPTIONS) -
	touch $(RT25USB_DIR)/.unpacked

$(RT25USB_DIR)/.patched: $(RT25USB_DIR)/.unpacked
	(cd $(RT25USB_DIR); QUILT_PATCHES=$(BASE_DIR)/package/ralink-usb-wifi $(QUILT) push -a)
	echo $(BASE_DIR)/package/ralink-usb-wifi > $@

$(RT25USB_MODULE): $(BASE_DIR)/uImage $(RT25USB_DIR)/.patched
	$(MAKE) -C "$(LINUX_DIR)" CROSS_COMPILE="$(KERNEL_CROSS)" ARCH=$(ARCH) CC="$(TARGET_CC)" EXTRA_CFLAGS="$(TARGET_CFLAGS)" SUBDIRS="$(RT25USB_DIR)" modules
	touch $@

$(RT25USB_TARGET_MODULE): $(RT25USB_MODULE)
	$(MAKE) -C "$(LINUX_DIR)" CROSS_COMPILE="$(KERNEL_CROSS)" ARCH=$(ARCH) CC="$(TARGET_CC)" EXTRA_CFLAGS="$(TARGET_CFLAGS)" SUBDIRS="$(RT25USB_DIR)" INSTALL_MOD_PATH="$(TARGET_DIR)" modules_install
	cp $< $@

$(RT25USB_TARGET_CONF):
	(grep -q rt2570 $(TARGET_DIR)/etc/modprobe.conf || \
	 echo -e 'alias rausb0 rt2570\n' >> $(TARGET_DIR)/etc/modprobe.conf)
	(grep -q rausb0 $(TARGET_DIR)/etc/network/interfaces || \
	 echo -e '\nauto rausb0\niface rausb0 inet dhcp\n	pre-up /sbin/iwconfig $$IFACE essid any txpower 100mW\n' >> $(TARGET_DIR)/etc/network/interfaces)
	touch $@

ralink-usb-wifi: uclibc $(RT25USB_TARGET_MODULE) $(RT25USB_TARGET_CONF)

ralink-usb-wifi-clean:
	rm $(RT25USB_TARGET_CONF) $(RT25USB_TARGET_MODULE)
	grep -v rausb0 $(TARGET_DIR)/etc/modprobe.conf > $(TARGET_DIR)/etc/modprobe-rewrite.conf && mv $(TARGET_DIR)/etc/modprobe-rewrite.conf $(TARGET_DIR)/etc/modprobe.conf
	grep -v rausb0 $(TARGET_DIR)/etc/network/interfaces > $(TARGET_DIR)/etc/network/interfaces-rewrite && mv $(TARGET_DIR)/etc/network/interfaces-rewrite $(TARGET_DIR)/etc/network/interfaces
	-$(MAKE) -C $(RT25USB_DIR) clean

ralink-usb-wifi-dirclean:
	rm -rf $(RT25USB_DIR)

#############################################################
#
# Toplevel Makefile options
#
#############################################################
ifeq ($(strip $(BR2_PACKAGE_RT25USB)),y)
TARGETS+=ralink-usb-wifi
endif
